package main

import (
	"fmt"
	"github.com/golang/protobuf/proto"
	"protobuf/userService"
)

func main() {
	//创建一个Go的数据结构（该类型，在proto文件编译后的Go文件中定义了。所以需要引入包）
	u := &userService.UserInfo{
		Name:  "tom",
		Age:   20,
		Hobby: []string{"唱歌", "跳舞"},
	}
	fmt.Printf("%#v\n", u) //userService.UserInfo{state:impl.MessageState{NoUnkeyedLiterals:pragma.NoUnkeyedLiterals{}, DoNotCompare:pragma.DoNotCompare{}, DoNotCopy:pragma.DoNotCopy{}, atomicMessageInfo:(*impl.MessageInfo)(0xc00010c2c0)}, sizeCache:0, unknownFields:[]uint8(nil), Name:"tom", Age:20, Hobby:[]string{"唱歌", "跳舞"}}
	fmt.Println(u.GetName())

	//序列化（结构体 --> 为protobuf格式）
	data, err := proto.Marshal(u)
	if err != nil {
		fmt.Printf("序列化失败，err-->%v", err)
		return
	}
	fmt.Println(data) //[10 3 116 111 109 16 20 26 6 229 148 177 230 173 140 26 6 232 183 179 232 136 158]

	//反序列化（protobuf格式 --> 结构体）
	var user userService.UserInfo
	proto.Unmarshal(data, &user)
	fmt.Printf("%#v\n", user) //userService.UserInfo{state:impl.MessageState{NoUnkeyedLiterals:pragma.NoUnkeyedLiterals{}, DoNotCompare:pragma.DoNotCompare{}, DoNotCopy:pragma.DoNotCopy{}, atomicMessageInfo:(*impl.MessageInfo)(0xc00010c2c0)}, sizeCache:0, unknownFields:[]uint8(nil), Name:"tom", Age:20, Hobby:[]string{"唱歌", "跳舞"}}

}
